package com.mall.user.services;

import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * @program: cs-mall_35th
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-14 14:59
 **/
@Slf4j
@Component
@Service
public class UserLoginServiceImpl implements ILoginService {

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest request) {

        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        String token = request.getToken();
        log.info("UserLoginService.validTokenImpl获取的token：" + token);
        try {
            // 检验token的合法性，解析token把userInfo放入response
            request.requestCheck();
            // 合法之后从token中取出msg
            String userInfo = JwtTokenUtils.builder().token(token).build().freeJwt();
            log.info("解析出的userInfo为：" + userInfo);
            checkAuthResponse.setUserInfo(userInfo);
        } catch (Exception e) {
            log.info("token数据不合法");
            checkAuthResponse.setCode(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getCode());
            checkAuthResponse.setMsg(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
            return checkAuthResponse;
        }

        checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return checkAuthResponse;

    }
}
